package cn.songdragon.zboschina;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.songdragon.zboschina.crawler.ProxyFlightCrawlerExecutorService;
import cn.songdragon.zboschina.proxy.ProxySingleton;

public class FlightCrawlerMain {

	private static final Logger logger = LoggerFactory.getLogger(FlightCrawlerMain.class);

	public static void main(String[] args) throws IOException, InterruptedException {
		String ctripUrl = "http://flights.ctrip.com/domesticsearch/search/SearchFirstRouteFlights?DCity1=SHA&ACity1=XNN&SearchType=S&DDate1=2016-04-05&IsNearAirportRecommond=0";

		int totalCount = 1000;

		long startTime = System.currentTimeMillis();
		ProxyFlightCrawlerExecutorService pfces = new ProxyFlightCrawlerExecutorService();
		for (int i = 0; i < totalCount; i++) {
			pfces.getUrl(ctripUrl);
		}
		while (pfces.getCompletedTaskCount() != totalCount) {
			Thread.sleep(3000);
			logger.info("Current Success count=" + pfces.getSuccessResponseCount() + "/Total Complete="
					+ pfces.getCompletedTaskCount());
		}

		long endTime = System.currentTimeMillis();
		pfces.shutdown();
		int successResponse = pfces.getSuccessResponseCount();
		int count = pfces.getResponseCount();
		logger.info(
				"Success ratio=" + successResponse + "/" + totalCount + "=" + (successResponse / (double) totalCount));
		logger.info("Response with correct content count=" + successResponse);
		logger.info("Response count=" + count);
		logger.info("Duration=" + (endTime - startTime) / 1000.0 + "s");
		logger.info(
				"Left avaliable proxies count=" + ProxySingleton.getProxyManagerInstance().getAvaliableProxyCount());

	}
}
